<?php
namespace Home\Controller;
use Think\Controller;
class RecordController extends HomeController {
    public function _initialize()
    {
        parent::_initialize();




        // 底部导航按钮状态
        if ( I('get.user','') ) {
            $this->assign('nav',3);
        } elseif ( ACTION_NAME == 'add' ){
            $this->assign('nav',2);
        } else {
            $this->assign('nav',1);
        }
    }

    // 舆情随手拍上传页面
    public function add(){
        // 需要微信登录
        if ( !check_login($this) ){
            $this->display('Public/need_wechat');
            exit;
        }

        // 固定的分类信息
        $category = $this['Category']->info(['id'=>33]);

        if ( IS_POST ) {

            // 获取当前模型信息
            $model = $this['Model']->info(['id'=>$category['model_id']]);

            // 当前用户uid赋值
            $_POST['user_id'] = session('user.uid');

            $res = $this[$model['name']]->update();
            if(!$res){
                $this->error($this[$model['name']]->getError());
            }else{
                $this->success($_POST['id']?'更新成功':'新增成功', U('Record/index',['user'=>1]));
            }
        } else {
            $this->assign('meta_title',$category['title']);
            $this->display();
        }
    }

    // 舆情随手拍 用户记录查看列表
    public function index($user = null){
        // 需要登录
        if ( !check_login($this) ){
            echo '需要登录';
            exit;
        }

        // 固定的分类信息
        $category = $this['Category']->info(['id'=>33]);

        // 模型信息
        $model = $this['Model']->info(['id'=>$category['model_id']]);

        $list = $this[$model['name']]->get(['user_id'=>session('user.uid')]);
        $this->assign('meta_title','我的记录');

        foreach($list as &$value){
            // 获取封面图片
            if ( $value['images'] ) {
                $images_id = preg_split('/[,]+/', trim($value['images'], ",;\r\n"));
                $value['cover_id'] = $images_id[0];
            }
            // 获取问题标题
            if ( $value['question_id'] ) {
                $qustion = $this['Question']->info(['id'=>$value['question_id']]);
                $value['question'] = $qustion;
            }
        }

        $this->assign('list',$list);
        $this->assign('title','舆情记录');

        $this->display('index_text');
    }
}